package chapter15;

import chapter11.Role;
import chapter13.UserService;
import chapter15.pojo.RoleParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @create 2018-04-19 上午9:50
 */
@Controller
@RequestMapping("/params")
public class ParamsController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ParamsController.class);

    @Autowired
    private UserService userService;

    @RequestMapping("/commonParams")
    public ModelAndView commonParams(String roleName, String note) {
        LOGGER.info("roleName=>{}", roleName);
        LOGGER.info("note=>{}", note);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("role");
        return mv;
    }

    @RequestMapping("/commonParamPojo")
    public ModelAndView commonParamPojo(RoleParams params) {
        LOGGER.info("roleName=>{}", params.getRoleName());
        LOGGER.info("note=>{}", params.getNote());
        ModelAndView mv = new ModelAndView();
        mv.setViewName("role");
        return mv;
    }

    @RequestMapping("/getRole/{id}")
    public ModelAndView pathVariable(@PathVariable("id") Long id) {
        Role role = userService.getRole(id);
        ModelAndView mv = new ModelAndView();
        mv.addObject(role);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }

    @RequestMapping(value = "/findRoles", method = RequestMethod.POST)
    public ModelAndView findRoles(@RequestBody RoleParams roleParams) {
        LOGGER.info("roleParams:{}", roleParams.toString());
        Role role = userService.getRole(1L);
        List<Role> roles = new ArrayList<>();
        roles.add(role);
        ModelAndView mv = new ModelAndView();
        mv.addObject(roles);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }

    /*通过javatscript序列化参数并返回*/
    @RequestMapping("/addRole")
    public ModelAndView findRoles(String roleName, String note) {
        LOGGER.info("roleName=>{}", roleName);
        LOGGER.info("note=>{}", note);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("addRole");
        return mv;
    }

    /*通过Model实现重定向*/
    @RequestMapping("/addRole2")
    public String addRole2(Model model, String roleName, String note) {
        Role role = new Role();
        role.setRoleName(roleName);
        role.setNote(note);
        userService.addUser(role);
        model.addAttribute("roleName", roleName);
        model.addAttribute("note", note);
        model.addAttribute("id", role.getRoleId());
        return "redirect:./showRoleJsonInfo";
    }

    /*通过ModelAndView实现重定向*/
    @RequestMapping("/addRole3")
    public ModelAndView addRole3(ModelAndView mv, String roleName, String note) {
        Role role = new Role();
        role.setRoleName(roleName);
        role.setNote(note);
        userService.addUser(role);
        mv.addObject("roleName", roleName);
        mv.addObject("note", note);
        mv.addObject("id", role.getRoleId());
        mv.setViewName("redirect:./showRoleJsonInfo");
        return mv;
    }

    /*通过重定向传递POJO*/
    @RequestMapping("/addRole4")
    public String addRole4(RedirectAttributes ra, Role role) {
        userService.addUser(role);
        ra.addFlashAttribute("role", role);
        return "redirect:./showRoleJsonInfo2";
    }

    @RequestMapping("/showRoleJsonInfo")
    public ModelAndView showRoleJsonInfo(Long id, String roleName, String note) {
        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        mv.addObject("id", id);
        mv.addObject("roleName", roleName);
        mv.addObject("note", note);
        return mv;
    }

    @RequestMapping("/showRoleJsonInfo2")
    public ModelAndView showRoleJsonInfo2(Role role) {
        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        mv.addObject("role", role);
        return mv;
    }
}
